<template>
	<view>
		<!-- #ifdef APP-PLUS || H5 -->
		<student-index ref="student" v-if="type == 2"></student-index>
		<coach-index ref="coach" v-else></coach-index>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<student-index ref="student"></student-index>
		<!-- #endif -->
	</view>
</template>
<script setup>
import { ref, getCurrentInstance, computed, watch } from 'vue';
import { onShow, onLoad } from '@dcloudio/uni-app';
import getConfig from '@/utils/getConfig'
import { getUserInfo } from '@/api/user.js';
// #ifdef APP-PLUS || H5
import CoachIndex from './coach/index/index';
// #endif
import StudentIndex from './student/index/index';
import { switchUserType } from '@/api/index';
import uniIm from '@/uni_modules/uni-im/lib/main.js';
// #ifdef APP-PLUS || H5
const type = uni.getStorageSync('start-state');
// #endif
// #ifdef MP-WEIXIN
const type = 2;
uni.setStorageSync('start-state', 2)
// #endif
const { proxy: that } = getCurrentInstance();

const student = ref(''),
	coach = ref('');

const unreadMsgCount = computed(() => {
	return uniIm.conversation.unreadCount();
});
watch(unreadMsgCount, (newVal, oldVal) => {
	// #ifdef APP
	plus.runtime.setBadgeNumber(newVal);
	// #endif

	if (newVal == 0) {
		return;
		//起冲突暂时不用
		uni.removeTabBarBadge({
			index: 2,
			complete: (e) => {
				// console.log(e)
			}
		});
	} else {
		uni.setTabBarBadge({
			index: 2,
			text: newVal + '',
			complete: (e) => {
				// console.log(e)
			}
		});
	}
});

//提前获取im数据，防止角标不显示
onLoad(() => {
	setTimeout(() => {
		uniIm.conversation.loadMore();
		switchUserType(uni.getStorageSync('start-state'));
	}, 500);
});

let icons = [];
if (type == 2)
	icons = [
		{
			iconPath: '/static/tabbar-icon/student/index.png',
			selectedIconPath: '/static/tabbar-icon/student/indexs.png',
			text: '首页'
		},
		{
			iconPath: '/static/tabbar-icon/student/find.png',
			selectedIconPath: '/static/tabbar-icon/student/finds.png',
			text: '发现'
		},
		{
			iconPath: '/static/tabbar-icon/student/msg.png',
			selectedIconPath: '/static/tabbar-icon/student/msgs.png',
			text: '消息'
		},
		{
			iconPath: '/static/tabbar-icon/student/train.png',
			selectedIconPath: '/static/tabbar-icon/student/trains.png',
			text: '训练'
		},
		{
			iconPath: '/static/tabbar-icon/student/mine.png',
			selectedIconPath: '/static/tabbar-icon/student/mines.png',
			text: '我的'
		}
	];
else
	icons = [
		{
			iconPath: '/static/tabbar-icon/home-d.png',
			selectedIconPath: '/static/tabbar-icon/home-a.png',
			text: '首页'
		},
		{
			iconPath: '/static/tabbar-icon/player-d.png',
			selectedIconPath: '/static/tabbar-icon/player-a.png',
			text: '学员'
		},
		{
			iconPath: '/static/tabbar-icon/message-d.png',
			selectedIconPath: '/static/tabbar-icon/message-a.png',
			text: '消息'
		},
		{
			iconPath: '/static/tabbar-icon/system-d.png',
			selectedIconPath: '/static/tabbar-icon/system-a.png',
			text: '体系'
		},
		{
			iconPath: '/static/tabbar-icon/my-d.png',
			selectedIconPath: '/static/tabbar-icon/my-a.png',
			text: '我的'
		}
	];

onShow(() => {
	setTimeout(() => {
		if (type == 2) student.value.showFun();
		else coach.value.showFun();
		icons.forEach((item, index) => {
			uni.setTabBarItem({
				index,
				iconPath: item.iconPath,
				selectedIconPath: item.selectedIconPath,
				text: item.text
			});
		});
		// #ifdef MP-WEIXIN
		student.value.showFun();
		// #endif
	});
});
// getConfig()
</script>
<style>
page {
	background: #f5f5f5;
}
</style>
